package com.tancire.framework.web.service;

import com.tancire.common.constant.Constants;
import com.tancire.common.enums.UserSource;
import com.tancire.common.exception.ServiceException;
import com.tancire.common.utils.ServletUtils;
import org.springframework.stereotype.Service;

/**
 * 请求头工具
 *
 * @author chenchao
 * @version 1.0
 * @since 2024/9/23 17:10
 */
@Service
public class HeaderService {

    /**
     * 获取当前请求头数据
     *
     * @return
     */
    public int getCurrentUserSource() {
        try {
            String us = ServletUtils.getRequest().getHeader(Constants.TANCIRE_SOURCE_KEY);
            return Integer.parseInt(us);
        } catch (Exception e) {
            throw new ServiceException("非法请求");
        }
    }

    /**
     * 获取当前请求头数据
     *
     * @return
     */
    public UserSource getSource() {
        int currentUserSource = getCurrentUserSource();
        if (currentUserSource == UserSource.SYSTEM.getCode()) {
            return UserSource.SYSTEM;
        } else if (currentUserSource == UserSource.MEMBER.getCode()) {
            return UserSource.MEMBER;
        } else {
            throw new ServiceException("非法请求");
        }
    }



}
